package com.zhutengwei.android.zuji.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zhutengwei.android.zuji.R;
import com.zhutengwei.android.zuji.db.TableDao;
import com.zhutengwei.android.zuji.model.UserModel;

/**
 * Created by Administrator on 2016/12/18.
 */
public class RegisterActivity extends BaseActivity {

    private final String TAG = "RegisterActivity";
    private EditText name;
    private EditText address;
    private EditText email;
    private EditText mobile;
    private EditText password;
    private EditText reEnterPassword;
    private Button register;
    private TextView login;
    private TableDao dao;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //初始化页面控件
        name = (EditText) findViewById(R.id.input_name);
        address = (EditText) findViewById(R.id.input_address);
        email = (EditText) findViewById(R.id.input_email);
        mobile = (EditText) findViewById(R.id.input_mobile);
        password = (EditText) findViewById(R.id.input_password);
        reEnterPassword = (EditText) findViewById(R.id.input_reEnterPassword);
        register = (Button) findViewById(R.id.btn_register);
        login = (TextView) findViewById(R.id.link_login);
        //注册按钮的监听事件
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(RegisterActivity.this,
                        LoginActivity.class);
                startActivity(i);
            }
        });

    }

    public void register(){
        Log.d(TAG, "Register");

        if (!validate()) {
            onSignupFailed();
            return;
        }

        register.setEnabled(false);

        final ProgressDialog progressDialog = new ProgressDialog(RegisterActivity.this,
                R.style.AppTheme_Dark_Dialog);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("创建帐户中...");
        progressDialog.show();

        String nameText = name.getText().toString();
        String addressText = address.getText().toString();
        String emailText = email.getText().toString();
        String mobileText = mobile.getText().toString();
        String passwordText = password.getText().toString();
        String reEnterPasswordText = reEnterPassword.getText().toString();

        //添加用户模型
        UserModel user = new UserModel();
        user.setNickname(nameText);
        user.setAddress(addressText);
        user.setEmail(emailText);
        user.setPhonenum(mobileText);
        user.setUserpass(passwordText);
        dao = new TableDao(this);
        dao.addUser(user);
        new Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        // On complete call either onSignupSuccess or onSignupFailed
                        // depending on success
                        onSignupSuccess();
                        // onSignupFailed();
                        progressDialog.dismiss();
                    }
                }, 3000);
    }

    public void onSignupSuccess() {
        register.setEnabled(true);
        setResult(RESULT_OK, null);
        finish();
    }

    public void onSignupFailed() {
        Toast.makeText(getBaseContext(), "注册失败", Toast.LENGTH_SHORT).show();

        register.setEnabled(true);
    }

    public boolean validate() {
        boolean valid = true;

        String nameText = name.getText().toString();
        String addressText = address.getText().toString();
        String emailText = email.getText().toString();
        String mobileText = mobile.getText().toString();
        String passwordText = password.getText().toString();
        String reEnterPasswordText = reEnterPassword.getText().toString();

        if (nameText.isEmpty() || nameText.length() < 3) {
            name.setError("至少三个字符");
            valid = false;
        } else {
            name.setError(null);
        }

        if (addressText.isEmpty()) {
            address.setError("请输入有效的地址");
            valid = false;
        } else {
            address.setError(null);
        }


        if (emailText.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(emailText).matches()) {
            email.setError("请输入有效的邮箱地址");
            valid = false;
        } else {
            email.setError(null);
        }

        if (mobileText.isEmpty() || mobileText.length()!=11) {
            mobile.setError("请输入有效的手机号");
            valid = false;
        } else {
            mobile.setError(null);
        }

        if (passwordText.isEmpty() || passwordText.length() < 4 || passwordText.length() > 10) {
            password.setError("请输入4到10个字母或数字");
            valid = false;
        } else {
            password.setError(null);
        }

        if (reEnterPasswordText.isEmpty() || reEnterPasswordText.length() < 4 || reEnterPasswordText.length() > 10 || !(reEnterPasswordText.equals(passwordText))) {
            reEnterPassword.setError("两次密码不一致");
            valid = false;
        } else {
            reEnterPassword.setError(null);
        }

        return valid;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dao.close();
    }
}
